﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DemoforGit
{
    public partial class FormMain : Form
    {
        private Game game;
        private object block;

        public FormMain()
        {
            InitializeComponent();
        }

        private void gameArea_Load(object sender, EventArgs e)
        {
            //初始化
            game = new Game();
            game.gameChanged += Game_gameChanged;
        }

        private void Game_gameChanged()
        {
           if (game!=null)
            {
                gameArea.Invalidate();
            }
           if (game.GameOver()== true)
            {
              
                //游戏结束了
                MessageBox.Show("Game Over!");
                //重置游戏
                game = new Game();
                game.gameChanged += Game_gameChanged;
            }
        }

        private void gameArea_Paint(object sender, PaintEventArgs e)
        {
            if (game !=null)
            {
                game.Draw(e.Graphics, this.gameArea.Size);
            }
        }

        private void FormMain_KeyDown(object sender, KeyEventArgs e)
        {
            if (game != null)
            {
                //传递按键值给game
                game.KeyDown(e.KeyCode.ToString());
                gameArea.Invalidate();
               
            }
        }

        private void btnStartGame_Click(object sender, EventArgs e)
        {
            game = new Game();
            game.gameChanged += Game_gameChanged;
        }

        private void btnAboutMe_Click(object sender, EventArgs e)
        {
            FormAboutme frm = new FormAboutme();
            frm.ShowDialog();
        }
    }
}
